program containercmd;

{$APPTYPE CONSOLE}

uses
  Windows,
  Classes,
  SysUtils,
  WSGIContainerConst in '../lib/WSGIContainerConst.pas',
  ContainerUtils in '../lib/ContainerUtils.pas';

procedure ShowUsage;
begin
  WriteLn(#10'Usage:  containercmd -mode [-option] [window] [file]');
  WriteLn('mode:   py pyf2 pyf js jsf openpy openjs');
  WriteLn('option: caption focused');
end;

var
  slCmdText: TStringList;
  hWindow: HWND;
  strCmd: String;
  wmCmd: Integer;
  FileParamPos: Integer;
  WndCaptionPos: Integer;
  strWndCaption: PChar;

begin
  FileParamPos := ParamCount;
  WndCaptionPos := ParamCount;

  slCmdText := TStringList.Create;
  try
    try
      if FindCmdLineSwitch('py') then
      begin
        wmCmd := WMCMD_PYTHON;
        repeat
          ReadLn(strCmd);
          slCmdText.Add(strCmd);
        until length(strCmd) = 0;
      end
      else if FindCmdLineSwitch('pyf2') then
      begin
        Dec(WndCaptionPos);
        wmCmd := WMCMD_PYTHONPROCFILE;
        slCmdText.Text := '';
      end
      else if FindCmdLineSwitch('pyf') then
      begin
        Dec(WndCaptionPos);
        wmCmd := WMCMD_PYTHON;
        if ParamCount < FileParamPos then
        begin
          WriteLn('Arguments Error.');
          Exit;
        end;
        if not FileExists(ParamStr(FileParamPos)) then
        begin
          WriteLn(Format('File not exists "%s"', [ParamStr(2)]));
          Exit;
        end;
        slCmdText.LoadFromFile(ParamStr(FileParamPos));
      end
      else if FindCmdLineSwitch('js') then
      begin
        wmCmd := WMCMD_JAVASCRIPT;
        repeat
          ReadLn(strCmd);
          slCmdText.Add(strCmd);
        until length(strCmd) = 0;
      end
      else if FindCmdLineSwitch('jsf') then
      begin
        Dec(WndCaptionPos);
        wmCmd := WMCMD_JAVASCRIPT;
        if ParamCount < FileParamPos then
        begin
          WriteLn('Arguments Error.');
          Exit;
        end;
        if not FileExists(ParamStr(FileParamPos)) then
        begin
          WriteLn(Format('File does not exists "%s"', [ParamStr(2)]));
          Exit;
        end;
        slCmdText.LoadFromFile(ParamStr(FileParamPos));
      end
      else if FindCmdLineSwitch('openpy') then
      begin
        Dec(WndCaptionPos);
        wmCmd := WMCMD_OPENFILEPY;
        if ParamCount < FileParamPos then
        begin
          WriteLn('Arguments Error.');
          Exit;
        end;
        if not FileExists(ParamStr(FileParamPos)) then
        begin
          WriteLn(Format('File does not exists "%s"', [ParamStr(2)]));
          Exit;
        end;
        slCmdText.Text := ExpandFileName(ParamStr(FileParamPos));
      end
      else if FindCmdLineSwitch('openjs') then
      begin
        Dec(WndCaptionPos);
        wmCmd := WMCMD_OPENFILEJS;
        if ParamCount < FileParamPos then
        begin
          WriteLn('Arguments Error.');
          Exit;
        end;
        if not FileExists(ParamStr(FileParamPos)) then
        begin
          WriteLn(Format('File does not exists "%s"', [ParamStr(2)]));
          Exit;
        end;
        slCmdText.Text := ExpandFileName(ParamStr(FileParamPos));
      end
      else if ParamCount = 1 then
      begin
        FileParamPos := 1;
        wmCmd := WMCMD_OPENFILEJS;
        if not FileExists(ParamStr(FileParamPos)) then
        begin
          ShowUsage;
          Exit;
        end;
        slCmdText.Text := ExpandFileName(ParamStr(FileParamPos));
      end
      else
      begin
        ShowUsage;
        Exit;
      end;

      if FindCmdLineSwitch('caption') then
      begin
        strWndCaption := PChar(ParamStr(WndCaptionPos));
        hWindow := FindWindow(nil, strWndCaption);
      end
      else
      begin
        hWindow := FindWindow('TWSGIContainerForm', nil);
      end;

      if hWindow = 0 then
      begin
        WriteLn('WSGI Container does not found.');
        Exit;
      end;

      CopyDataToContainer(hWindow, wmCmd, slCmdText.Text, FindCmdLineSwitch('focused'));

    except
      on E: Exception do
      begin
        WriteLn(E.Message);
      end;
    end;
  finally
    slCmdText.Free;
  end;
end.
